<?php
/**
 * Controller_Helper
 * @create 2013-06-23 00:44:47
 */
class Controller_Helper extends Zend_Controller_Action
{
    protected $_validator;
    
    protected $_page;
    
    protected $_num;
    
    protected $_orderby;
    
    protected $_author;
    
    protected $_date;
    
    protected $_language;
    
    protected $_url;
    
    public function init()
    {
        parent::init();
        $this->_validator = new Zend_Validate_Int();
        $this->_page = (int)$this->_request->getParam('page');
        $this->_author = (int)$this->_request->getParam('author');
        $this->_date = $this->_request->getParam('date');
        if (empty($this->_page) || !$this->_validator->isValid($this->_page)) {
            $this->_page = 1;
        }
        
        $url = $_SERVER['REQUEST_URI'];
        $urlElement = explode('?', $url);
        $this->_url = $urlElement[0];
        
        $lang = $this->_request->getParam('lang');
        if (empty($lang)) {
            if (isset($_COOKIE['language'])) {
                $lang = $_COOKIE['language'];
            }
        } else {
            $this->_dealWithLanguage($lang);
            $this->_redirect($this->_url);
        }
        $this->_dealWithLanguage($lang);
    }
    
    private function _dealWithLanguage($lang)
    {
        if ($lang == 'zh') {
            $path = APPLICATION_CONFIG_PATH . '/zh.ini';
        } elseif ($lang == 'pt') {
            $path = APPLICATION_CONFIG_PATH . '/pt.ini';
        } else {
            $lang = 'en';
            $path = APPLICATION_CONFIG_PATH . '/en.ini';
        }
        setcookie('language', $lang, time() + 2592000, '/');
        $this->_language = $lang;
        $config = new Zend_Config_Ini($path, APPLICATION_ENV);
        $registry = Zend_Registry::getInstance()->set('lang', $config);
        $this->view->footer = Zend_Registry::get('lang')->model->bottom;
        $this->view->language = $this->_language;
    }
    
    /**
     * @param unknown $classType
     */
    protected function _getSpecialInfo($classType)
    {
        error_reporting(0);
        $this->view->title = Zend_Registry::get('config')->type->$classType->title;
        $this->view->controller = $classType;
        $this->view->url = $this->_url;
    }
    
    protected function _getColumnIndex()
    {
        $column = $this->_request->getParam('column');
        if (!$this->_validator->isValid($column)) {
            $column = 0;
        }
        $this->view->column = $column;
    }
}
